const {
  http
} = require("../../../../utils/apiutils")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    show1: false,
    sIndex: 0,
    sh: "300",
    dataList: [],
    checked1: false,
    checked2: false,
    titlist: [{
      name: "已报名",
      value: 1,
    }, {
      name: "工作中",
      value: 2,
    }, {
      name: "结算中",
      value: 3,
    }]
  },
  onclickAdd: function () {
    if (this.data.sIndex < this.data.titlist.length - 1) {
      this.setData({
        sIndex: this.data.sIndex + 1
      })
    } else {
      this.setData({
        sIndex: 0
      })
    }
    this.getDataList();

  },
  sureClick: function () {
    //暂停招聘
    if (this.data.workInfo.status != 10) {
      var that = this;
      http.ztfactoryJobId(this.data.workInfo.id,
        function (res) {
          wx.hideLoading()
          console.log("-----暂停成功---", res);
          if (res.success) {
            that.data.workInfo.status == 10;
            getApp().globalData.workInfo.status = 10;
            that.setData({
              show1: false,
              workInfo: that.data.workInfo,
            })
            wx.showToast({
              title: res.message,
              icon: "none",
            })
          } else {
            wx.showToast({
              title: res.message,
              icon: "none",
            })
          }
        },
        function (err) {
          wx.hideLoading()
        })

    }
    //开启招聘
    else if (this.data.workInfo.status == 10) {
      var that = this;
      http.kqfactoryJobId(this.data.workInfo.id,
        function (res) {
          wx.hideLoading()
          console.log("-----开启招聘成功---", res);
          if (res.success) {
            that.data.workInfo.status == 2
            that.setData({
              show1: false,
              workInfo: that.data.workInfo,
            })
            wx.showToast({
              title: res.message,
              icon: "none",
            })
          } else {
            wx.showToast({
              title: res.message,
              icon: "none",
            })
          }
        },
        function (err) {
          wx.hideLoading()
        })
    } else {
      wx.showToast({
        title: '已完成无法无需暂停招聘',
        icon: "none",
      })
    }


  },
  onclickZP: function () {
    this.setData({
      show1: true,
      sm: this.data.workInfo.status == 10 ? "是否确定要恢复招聘" : "是否确定要暂停招聘"
    })
  },
  onclickre: function () {
    if (this.data.sIndex == 0) {
      this.setData({
        sIndex: this.data.titlist.length - 1
      })
    } else {
      this.setData({
        sIndex: this.data.sIndex - 1
      })
    }
    this.getDataList();
  },
  onclickmobile: function (e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  onclickJS2: function (e) {
    var arr = this.data.dataList
    var uid = arr.map(function (elem) {
      if (elem.checked) {
        return elem.id;
      }
    }).join(",")
    let status = e.currentTarget.dataset.status;
    console.log("-----", uid, status);
    if (uid && uid != "") {

      this.onlcickJS(uid, status)

    } else {
      wx.showToast({
        title: '请选择对应人员后进行操作',
        icon: "none",
      })
    }

  },
  onlcickly1: function (e) {
    let item = e.currentTarget.dataset.item;
    this.onlcickLY(item.id)
  },
  onlcickJS1: function (e) {
    let item = e.currentTarget.dataset.item;
    let status = e.currentTarget.dataset.status;
    this.onlcickJS(item.id, status)
  },
  onlcickLY: function (id) {

    // 参数 :临时工ID 多个用','分割
    //9结算 10审核
    var that = this;
    // var data = {
    //   factoryJobId:this.data.workInfo.id,
    //   uId:uId,
    //   status:status,
    // }
    var str = "/" + uId + "/" + this.data.workInfo.id
    http.updateHireStatus(str,
      function (res) {
        wx.hideLoading()
        console.log("-----岗位人员信息---", res);
        if (res.success) {
          that.getDataList();
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },
  onclickCopy: function () {
    wx.navigateTo({
      url: '/pages/home/pages/company/company?type=2',
    })
  },
  onclickKQ: function (e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/home/pages/calendar/calendar',
    })
  },
  onclickCheck: function () {
    wx.navigateTo({
      url: '/pages/home/pages/company/company?type=1',
    })
  },
  onlcickJS: function (uId, status) {

    // 参数 :临时工ID 多个用','分割
    //9结算 10审核
    var that = this;
    // var data = {
    //   factoryJobId:this.data.workInfo.id,
    //   uId:uId,
    //   status:status,
    // }
    var str = status + "/" + uId + "/" + this.data.workInfo.id
    http.updateFJStatus(str,
      function (res) {
        wx.hideLoading()
        console.log("-----岗位人员信息---", res);
        if (res.success) {
          that.getDataList();
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })


  },
  onChange: function (e) {
    let index = e.currentTarget.dataset.index;
    this.data.dataList[index].checked = !this.data.dataList[index].checked
    this.setData({
      dataList: this.data.dataList,
    })
  },
  onChange1: function (e) {

    var arr = this.data.dataList;
    arr.forEach(element => {
      element.checked = !this.data.checked1;
    });
    this.setData({
      checked1: !this.data.checked1,
      dataList: arr,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (result) => {
        console.log("----", result);
        var rpx = result.windowHeight * 750 / result.windowWidth - 97;
        this.setData({
          sh: rpx
        })
      },
    })
    this.setData({
      workInfo: getApp().globalData.workInfo
    })
    this.getDataList()
  },
  getDataList: function (st) {
    var that = this;
    // 1 已报名 4 工作中 6 结算
    var num = 1;
    if (this.data.sIndex == 0) {
      num = 1
    } else if (this.data.sIndex == 1) {
      num = 4
    } else if (this.data.sIndex == 2) {
      num = 6
    }
    var that = this;
    var data = {
      factoryJobId: this.data.workInfo.id,
      status: num,
    }
    http.applicantInPostId(data,
      function (res) {
        wx.hideLoading()
        console.log("-----岗位人员信息---", res);
        var arr = res.data.records;
        arr.forEach(element => {
          element.checked = false;
        });
        if (res.success) {
          that.setData({
            dataList: arr
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})